// 基类，建议所有自定义类均继承该类
export class Matrix{
    private _isValid:boolean = false;
    private _name:string = '';
    constructor(){
        this._isValid = true;
        this._name = Object.prototype.constructor.name;
    }

    public get name():string{return this._name;}

    public init(...args: any[]){
        var _len: number = args.length;
        switch(_len){
            case 0:this.onInit();break;
            case 1:this.onInit(args[0]);break;
            case 2:this.onInit(args[0], args[1]);break;
            case 3:this.onInit(args[0], args[1], args[2]);break;
            case 4:this.onInit(args[0], args[1], args[2], args[3]);break;
            case 5:this.onInit(args[0], args[1], args[2], args[3], args[4]);break;
            default : break;
        }
    }

    public destroy(){
        this._isValid = false;
        this.onDestroy();
        let self = this;
        self = null;
    }
    
    public get isValid():boolean{return this._isValid;}

    protected onInit(...param: any[]){
        // 子类实现
    }

    protected onDestroy(){
        // 子类实现
    }
}

// 单件
export class Singleton<T> extends Matrix{
    private _instance:T = null;
    constructor(){
        super();
    }

    public static getInstance<T>(o: { new(): T }): T {
        if (this.prototype._instance == null) {
            this.prototype._instance = new o();
        }
        return this.prototype._instance as T;
    }
}

/**
 * 网络数据处理
 * 仅仅只负责协议的加解密和心跳包的数据组织，派生类实现protected方法
 */
export interface PkgData  {proto:string,data:any}; // 解析后包结构定义
export abstract class SocketParser extends Matrix{
    protected _isBinary:boolean;
    constructor(binary:boolean){
        super()
        this._isBinary = binary
    }
    public get binary():boolean{return this._isBinary;}
    public encode(data:any):any{return this.onEncode(data);}
    public sendHeartbeat():void{this.onHeartbeat();}
    public message(proto:string,data:any):void{this.onMessage(proto,data);}
    public decode(data:any):PkgData{return this.onDecode(data);}

    protected abstract onEncode(data:any):any; // 封包
    protected abstract onDecode(data:any):PkgData; // 解包
    protected abstract onHeartbeat():void
    protected abstract onMessage(proto:string,data:any):any;
}

export const EventConst = {
    APP_SHOW:'sys_event_app_show',
    APP_HIDE:'sys_event_app_hide',
    APP_RESTART:'sys_event_app_restart',
    APP_TOP_TOUCH:'sys_event_top_layout_touch', // 顶层点击
    APP_CHANGE_I18N:'sys_event_change_i18n',    // 改变语言
    App_NATIVE_MESSAGE:'sys_event_native_message', // ios/android 底层回调
    HOT_UPDATE_MODULE:'sys_event_hotupdate_module',
    CHECK_MODULE:'sys_event_check_module', // 检测模块是否需要更新
    UPDATE_MODULE:'sys_event_update_module', // 更新模块
    DOWNLOAD_MODULE:'sys_event_download_module', // 下载模块
    FINISH_MODULE:'sys_event_finish_module',
    START_LOGIN:'sys_event_start_login',
    START_LOGOUT:'sys_event_start_logout',
    LOADING: 'director_event_loading', // 加载 
    ENTRY_SCENE: 'director_event_entry_scene', // 进入场景 
    SET_SKY_BOX_STYLE: 'scene_set_sky_box_style',
    SET_MAIN_VIEW:'director_event_set_main_view', // 设置主窗
    POP_VIEW: 'director_event_POP_view',// 弹框
    CLOSE_VIEW: 'director_event_close_view',// 关闭弹框窗口
    RESIZE_VIEWPORT:'director_event_resize_viewport', // 重置
    JUST_NODE_SIZE:'director_event_just_node_szie', // 调整节点尺寸
    POP_TIPS: 'director_event_pop_tips_msg',// 推送一条tips
    POP_MSG_BOX: 'director_event_pop_message_box', // 弹对话框(msg:string,btns:'yes|no',cb?:(btn)=>{})
    LOCK_SCREEN: 'director_event_lock_screen', // 锁定屏幕 true锁定，false 解开 (isLock:上/解锁,isRound:是否转菊花) 

    SOCKET_BIND:'sys_event_socket_bind',
    SOCKET_AGAIN_CONNETED:'sys_event_socket_again_connect', // 网络重连接 
    SOCKET_CONNETED:'sys_event_connected_has_socket',// url:string,cb:()=>void
    SOCKET_SEND:'sys_event_socket_send_msg',//data:{},cb:(data:any)=>void
    SOCKET_CLOSE:'sys_event_socket_close',
    SOCKET_CHANGE_CONNECTED:'sys_event_socket_change_connected',
    SOCKET_DISCONNECT:'sys_event_socket_disconnect',
    SOCKET_TRY_CONNECT:'sys_event_socket_try_connect',
    SOCKET_TRY_CONNECT_FAIL:'sys_event_socket_try_connect_fail',
    SOCKET_SYNCHRO_SVR_TIME:'sys_event_socket_synchro_svr_time',

    
    // 登录成功，返回服务器玩家信息
    LOGIN_SUCCESS: 'login_success_get_user_info',


    POP_NOTICE:'force_pop_notice', // 强制弹公告

    USE_PROP:'use_prop',  // 使用道具

    ADD_PROP_PACK:'add_goods_pack', // 向背包中添加一个道具
    REMOVE_PROP_PACK:'remove_goods_pack', // 删除背包中的指定数量物体
    UPDATE_PROP_NUMBER_PACK:'update_prop_number_pack', // 更新背包道具数量


    UPDATE_USER_DATA:'update_user_data', // 更新用户数据
    
};

// 2D视图层
export enum ViewLayer{
    Unknow=-1,
    Bottom,
    Pop,
    Mask,
    Top,
}

export enum ButtonFlag{
    Ok=0xaaa,
    Cancel=0xbbb,
    Other=0xccc,
}

export enum Location{
    LeftTop = 0, 
    Top,
    RightTop,
    Left,
    Center,
    Right,
    LeftBottom,
    Bottom,
    RightBottom
}

export interface AudioVol{
    music:number,
    effect:number,
}
export const LocalKey = {
    AUDIO:'local_audio',//
    LOGIN:'local_login',
    SET:'local_set',
    USER:'local_user',
    LANG:'local_language',
    WRITE_DIR:'native_write_path' // 本地写目录，热更使用
}

export const ModuleConst = {
    MASTER:'master',
}

export enum HotUpdateState{
    Skip=0,
    New,
    Update,
    Progress,
    Finish,
    Error,
}
